package cn.echo.hrm.service.impl;

import cn.echo.hrm.contants.CourseConstants;
import cn.echo.hrm.domain.Course;
import cn.echo.hrm.domain.CourseDetail;
import cn.echo.hrm.domain.CourseMarket;
import cn.echo.hrm.dto.CourseAddParamDTO;
import cn.echo.hrm.exception.Assert;
import cn.echo.hrm.exception.BusinessException;
import cn.echo.hrm.mapper.CourseMapper;
import cn.echo.hrm.service.ICourseDetailService;
import cn.echo.hrm.service.ICourseMarketService;
import cn.echo.hrm.service.ICourseService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxy
 * @since 2022-04-10
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private ICourseMarketService courseMarketService;

    @Autowired
    private ICourseDetailService courseDetailService;

    @Override
    @Transactional
    public void add(CourseAddParamDTO dto) {

        Course course = dto.getCourse();
        CourseMarket courseMarket = dto.getCourseMarket();
        CourseDetail courseDetail = dto.getCourseDetail();

        String name = course.getName();
        //条件对象
        Wrapper<Course> wrapper = new EntityWrapper();
        wrapper.eq("name",name);
        Course courseFromDB = selectOne(wrapper);
        Assert.assertNull(courseFromDB,"课程名重复");
        course.setStatus(CourseConstants.KEY_COURSE_STATUS_OFFLINE);
        course.setTenantId(26L);
        course.setTenantName("DBL");
        course.setUserId(1L);
        course.setUserName("yhptest1");
        //保存课程
        insert(course);

        //保存营销
        courseMarket.setId(course.getId());
        courseMarketService.insert(courseMarket);

        //保存详情
        courseDetail.setId(course.getId());
        courseDetailService.insert(courseDetail);

    }

    @Override
    public void onLineCourse(Long courseId) {
        Assert.assertNotNull(courseId,"请求错误 ");
        //判断是否没有上架
        Course course = selectById(courseId);
        Assert.assertNotNull(course,"请求错误");
        if(course.getStatus().intValue() != CourseConstants.KEY_COURSE_STATUS_OFFLINE){
            throw new BusinessException("课程已上线");
        }

        //修改状态
        course.setStatus(CourseConstants.KEY_COURSE_STATUS_ONLINE);
        updateById(course);

        //存入Es

    }
}
